package com.hotel.company.service;

import com.hotel.comm.bean.DubboReply;
import com.hotel.comm.bean.IReply;
import com.hotel.comm.bean.company.EjobCompanyUser;
import com.hotel.comm.service.company.ICompanyUserService;
import com.hotel.company.Constants;
import com.hotel.company.repository.IEjobCompanyUserRepository;
import com.hotel.core.utils.DateUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 企业用户service层
 * Created by frank on 15-6-16.
 */
@Service
public class CompanyUserServiceImpl implements ICompanyUserService {

    @Autowired
    IEjobCompanyUserRepository ejobCompanyUserRepository;

    @Override
    public IReply getById(Long companyUserId) {
        try {
            EjobCompanyUser ejobCompanyUser = this.ejobCompanyUserRepository.findOne(companyUserId);
            return new DubboReply(ejobCompanyUser);
        } catch (Exception e) {
            return new DubboReply(Constants.REPLY_CODE_COMPANY_USER_GET_BY_ID, Constants.REPLY_MSG_COMPANY_USER_GET_BY_ID);
        }
    }

    @Override
    public IReply save(EjobCompanyUser ejobCompanyUser) {
        try {
            /*ejobCompanyUser.setCreateDate(DateUtils.getCurrentDate());
            ejobCompanyUser.setUpdateDate(DateUtils.getCurrentDate());
            ejobCompanyUser.setPassword(DigestUtils.md5Hex(ejobCompanyUser.getPassword()));*/
            EjobCompanyUser persistUser = new EjobCompanyUser(ejobCompanyUser.getLoginName(), ejobCompanyUser.getPassword(), ejobCompanyUser.getEmail());
            EjobCompanyUser companyUser = this.ejobCompanyUserRepository.save(persistUser);
            return new DubboReply(companyUser);
        } catch (Exception e) {
            return new DubboReply(Constants.REPLY_CODE_SAVE_COMPANY_USER, Constants.REPLY_MSG_SAVE_COMPANY_USER);
        }
    }

    @Override
    public IReply update(EjobCompanyUser ejobCompanyUser) {
        try {
            EjobCompanyUser persitEjobCompanyUser = ejobCompanyUserRepository.findOne(ejobCompanyUser.getId());
            persitEjobCompanyUser.setLoginName(ejobCompanyUser.getLoginName());
            persitEjobCompanyUser.setPassword(DigestUtils.md5Hex(ejobCompanyUser.getPassword()));
            persitEjobCompanyUser.setUpdateDate(DateUtils.getCurrentDate());
//            BeanUtils.copyProperties(ejobCompanyUser, ejobCompanyUserVo);
            EjobCompanyUser companyUser = this.ejobCompanyUserRepository.save(persitEjobCompanyUser);
            return new DubboReply(companyUser);
        } catch (Exception e) {
            return new DubboReply(Constants.REPLY_CODE_SAVE_COMPANY_USER, Constants.REPLY_MSG_SAVE_COMPANY_USER);
        }
    }

    @Override
    public IReply delete(Long companyUserId) {
        try {
            this.ejobCompanyUserRepository.delete(companyUserId);
            return new DubboReply();
        } catch (Exception e) {
            return new DubboReply(Constants.REPLY_CODE_DELETE_COMPANY_USER, Constants.REPLY_MSG_DELETE_COMPANY_USER);
        }
    }

    @Override
    public IReply findByLoginName(String loginName) {
        try {
            EjobCompanyUser ejobCompanyUser = this.ejobCompanyUserRepository.findByLoginName(loginName);
            return new DubboReply(ejobCompanyUser);
        } catch (Exception e) {
            return new DubboReply(Constants.REPLY_CODE_COMPANY_USER_GET_BY_LOGINNAME, Constants.REPLY_MSG_COMPANY_USER_GET_BY_LOGINNAME);
        }
    }
}
